<?PHP
include "../../../mainfile.php";
$myts =& MyTextSanitizer::getInstance();

//UPDATE STATION INFO
if($_POST['action'] == 'station_info'){

//make sure only one billing email has been specified (for GPG encryption purposes)
$first_dot = strpos ($_POST['billing_recipient_Email'], '.');
$billing_email = substr ($_POST['billing_recipient_Email'], '0' ,($first_dot+4));

$first_dot = strpos ($_POST['non_billing_recipients'], '.');
$non_billing_email = substr ($_POST['non_billing_recipients'], '0' ,($first_dot+4));


$xoopsDB->query("UPDATE " . $xoopsDB->prefix('pledge_station_info') . " SET welcome = '".$myts->makeTboxData4Show($_POST['welcome'])."', stationLetters = '".$myts->makeTboxData4Show(strtoupper($_POST['stationLetters']))."', Billing_Recipient_Name = '".$myts->makeTboxData4Show($_POST['Billing_Recipient_Name'])."', billing_recipient_Email = '$billing_email', gpg_encrypt_info_4_billing = '".$_POST['gpg_encrypt_info_4_billing']."', Customer_Service_local_number = '".$myts->makeTboxData4Show($_POST['Customer_Service_local_number'])."', Customer_Service_toll_free_number = '".$myts->makeTboxData4Show($_POST['Customer_Service_toll_free_number'])."', Customer_Service_Email = '".$myts->makeTboxData4Show($_POST['Customer_Service_Email'])."', non_billing_recipients = '$non_billing_email', gpg_encrypt_info_4_non_billing = '".$_POST['gpg_encrypt_info_4_non_billing']."', use_ssl = '".$_POST['use_ssl']."'");
$_SESSION['messsage'] = "Your station info has been updated!";
unset($_SESSION['stationLetters']);//cause new settings to take effect for end user
redirect_header('index.php?op=config&currentID=0', 1, 'Your station info has been updated!');
}

//ADD NEW PREMIUM
if($_POST['action'] == 'add_premium'){

			//+++++++++++++++++++++++++++++++++++++++++
			//DO FORM VALIDATION ++++++++++++++++++++++
			//+++++++++++++++++++++++++++++++++++++++++
			$_SESSION['title'] = $_POST['title'];
			$_SESSION['description'] = $_POST['description'];
			$_SESSION['pledge_level'] = ereg_replace (',', '', (ereg_replace ('[^0-9,.]+', '', $_POST['pledge_level'])));
			$_SESSION['featured'] = $_POST['featured'];
			

			if(!$_POST['title'] || !$_POST['description'] || !$_SESSION['pledge_level']){
				$_SESSION['messsage'] = "WARNING! PREMIUM NOT ADDED.  Please complete all fields before submitting a new premium.";			
			}else{


						include_once '../../../class/uploader.php';
								$allowed_mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png');
								$maxfilesize = '15000';
								$maxfilewidth = '120';
								$maxfileheight = '120';
								$uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH . "/" . "modules/pledge/images/uploads", $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
								if ($uploader->fetchMedia($_POST["xoops_upload_file"][0])) {
									if (!$uploader->upload()) {
										$_SESSION['messsage'] = $uploader->getErrors();
									} else {
									
									//+++++++++++++++++++++++++++++++++++++++++
									//INSERT STUFF INTO DB ++++++++++++++++++++
									//+++++++++++++++++++++++++++++++++++++++++
									$fileName = $uploader->getSavedFileName();
									if($_SESSION['featured'] != 'true'){
										$_SESSION['featured'] = 'false';
									}
									$xoopsDB->query("INSERT INTO " . $xoopsDB->prefix('pledge_premiums') . " values ('0','".$_SESSION['title']."','$fileName','".$_SESSION['description']."','".$_SESSION['pledge_level']."','".$_SESSION['featured']."','true')");
										$_SESSION['messsage'] = 'You have successfully added the following premium: '.$_SESSION['title'];
										//echo 'Saved as: ' . $uploader->getSavedFileName() . '<br />';
										//echo 'Full path: ' . $uploader->getSavedDestination();
										
										unset($_SESSION['title'],$_SESSION['description'],$_SESSION['pledge_level'],$_SESSION['featured']);
									}
								} else {
										$_SESSION['messsage'] = $uploader->getErrors();
								} 
								
		}
		
		
	redirect_header('index.php?op=premiums&currentID=1', 1, $_SESSION['messsage']);
}


//################################
//EDIT EXISTING PREMIUM ##########
//################################

if($_POST['action'] == 'edit_premium'){
				unset($_SESSION['premiums']);
				unset($_SESSION['premiumsRetrieved']);


			$_SESSION['title'] = $_POST['title'];
			$_SESSION['description'] = $_POST['description'];
			$_SESSION['pledge_level'] = ereg_replace (',', '', (ereg_replace ('[^0-9,.]+', '', $_POST['pledge_level'])));
			$_SESSION['featured'] = $_POST['featured'];
			$_SESSION['active'] = $_POST['active'];
			
			if($_SESSION['featured'] != 'true'){
				$_SESSION['featured'] = 'false';
			}
			
			if($_SESSION['active'] != 'true'){
				$_SESSION['active'] = 'false';
			}
			
			
			
			
			if(!$_POST['title'] || !$_POST['description'] || !$_SESSION['pledge_level']){
				$_SESSION['messsage'] = "WARNING! PREMIUM NOT UPDATED.  Please complete all fields before submitting a new premium.";			
				unset($_SESSION['title'],$_SESSION['description'],$_SESSION['pledge_level'],$_SESSION['featured']);
				header("Location:index.php?ID=1&sub=2");
			}else{
					if($_FILES['uploadimage']['name']){
					//We have a new image
 						include_once '../../../class/uploader.php';
								$allowed_mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png');
								$maxfilesize = '15000';
								$maxfilewidth = '120';
								$maxfileheight = '120';
								$uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH . "/" . "modules/pledge/images/uploads", $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
								if ($uploader->fetchMedia($_POST["xoops_upload_file"][0])) {
									if (!$uploader->upload()) {
										$_SESSION['messsage'] = $uploader->getErrors();
									} else {
									
									//+++++++++++++++++++++++++++++++++++++++++
									//INSERT STUFF INTO DB ++++++++++++++++++++
									//+++++++++++++++++++++++++++++++++++++++++
									$fileName = $uploader->getSavedFileName();
									$xoopsDB->query("UPDATE " . $xoopsDB->prefix('pledge_premiums') . " SET title = '".$myts->makeTboxData4Show($_SESSION['title'])."', image = '$fileName', description = '".$myts->makeTboxData4Show($_SESSION['description'])."', pledge_level = '".$_SESSION['pledge_level']."', featured = '".$myts->makeTboxData4Show($_SESSION['featured'])."', active = '".$myts->makeTboxData4Show($_SESSION['active'])."' WHERE ID = '".$_POST['ID']."'");
										$_SESSION['messsage'] = 'You have successfully updated premium #'.$_POST['ID'].'.';
										//echo 'Saved as: ' . $uploader->getSavedFileName() . '<br />';
										//echo 'Full path: ' . $uploader->getSavedDestination();
										
										unset($_SESSION['title'],$_SESSION['description'],$_SESSION['pledge_level'],$_SESSION['featured']);
									}
								} else {
										$_SESSION['messsage'] = $uploader->getErrors();
								} 
								header("Location:index.php?ID=1&sub=0");
					}else{
					//KEEP SAME IMAGE
					$xoopsDB->query("UPDATE " . $xoopsDB->prefix('pledge_premiums') . " SET title = '".$myts->makeTboxData4Show($_SESSION['title'])."', description = '".$myts->makeTboxData4Show($_SESSION['description'])."', pledge_level = '".$_SESSION['pledge_level']."', featured = '".$myts->makeTboxData4Show($_SESSION['featured'])."', active = '".$myts->makeTboxData4Show($_SESSION['active'])."' WHERE ID = '".$_POST['ID']."'");
					unset($_SESSION['title'],$_SESSION['description'],$_SESSION['pledge_level'],$_SESSION['featured']);
					redirect_header('index.php?op=premiums&currentID=1', 1, 'You have successfully updated premium #'.$_POST['ID'].'.');
					}

								
		}
		
		
	
		
}elseif($_GET['action'] == 'delete'){
	$xoopsDB->query("DELETE FROM ".$xoopsDB->prefix("pledge_premiums")." WHERE ID=".$_GET['ID']."");
	redirect_header('index.php?op=premiums&currentID=1', 1, 'You have successfully deleted premium #'.$_GET['ID'].'.');
	
}



//########################################
// ADD NEW PLEDGE LEVEL ##################
//########################################
if($_POST['action'] == 'add_pledge_level'){
	unset($_SESSION['levels']);//clear out so end user will see changes
	if($_POST['pledge_level']==""){
	redirect_header('index.php?op=level&currentID=2', 1, 'Please enter an amount of the new pledge level before submitting!');
	}else{
		setlocale(LC_MONETARY, 'en_US');
		$_SESSION['pledge_level'] = ereg_replace (',', '', (ereg_replace ('[^0-9,.]+', '', $_POST['pledge_level'])));
		$xoopsDB->query("INSERT INTO " . $xoopsDB->prefix('pledge_levels') . " values ('0','".$_SESSION['pledge_level']."','".$_POST['description']."')");
		redirect_header('index.php?op=level&currentID=2', 1, 'You have succesfully added the following pledge level: '.money_format('%.0n', $_SESSION['pledge_level']));
		unset($_SESSION['pledge_level']);
	}

//######################################
//DELETE EXISTING PLEDGE LEVEL #########
//######################################
}elseif($_GET['action'] == 'delete_pledge_level'){
	unset($_SESSION['levels']);//clear out so end user will see changes
	$xoopsDB->query("DELETE FROM ".$xoopsDB->prefix("pledge_levels")." WHERE ID=".$_GET['ID']."");
		redirect_header('index.php?op=level&currentID=2', 1, 'You have successfully deleted the selected pledge level!');
}elseif($_POST['action'] == 'recent'){
//######################################
//UPDATE RECENT PLEDGES CONFIG #########
//######################################

		$num_2_display = ereg_replace (',', '', (ereg_replace ('[^0-9,.]+', '', $_POST['recent_number_2_show'])));
		$show_date = $_POST['recent_show_date'];
		$show_amount = $_POST['recent_show_amount'];
		$xoopsDB->query("UPDATE " . $xoopsDB->prefix('pledge_station_info') . " SET recent_number_2_show = '$num_2_display',  recent_show_date= '$show_date',  recent_show_amount = '$show_amount'");
	unset($_SESSION['recent_number_2_show']);
	
	
		redirect_header('index.php?op=recent&currentID=3', 1, 'You have successfully updated the recent pledges configuration!');
}elseif($_POST['action'] == 'recent_delete'){
//######################################
//DELETE RECENT PLEDGE NAMES #########
//######################################



	for ($i = 0; $i < $_POST['highnumber']; $i++) {
		if($_POST["delete$i"]){
				$xoopsDB->query("DELETE FROM " . $xoopsDB->prefix('pledge_recent') . " WHERE ID = '".$_POST["delete$i"]."'");

		}
	}

	unset($_SESSION['recent_number_2_show']);
	
		redirect_header('index.php?op=recent&currentID=3', 1, 'You have successfully deleted the selected pledges!');
}elseif($_POST['update_level']){
//######################################
//UPDATE PLEDGE LEVELS #########
//######################################
unset($_SESSION['levels']);//clear out so end user will see changes
	if($_POST['pledge_level']==""){
	redirect_header('index.php?op=level&currentID=2', 1, 'Please enter an amount of the new pledge level before submitting!');
	}else{
	
		//echo $_POST['pledge_level'] ." - " . $_POST['description']." - " . $_POST['update_level'];
	
		$_POST['pledge_level'] = ereg_replace (',', '', (ereg_replace ('[^0-9,.]+', '', $_POST['pledge_level'])));
		$xoopsDB->query("UPDATE " . $xoopsDB->prefix('pledge_levels') . " SET pledge_level = '".$_POST['pledge_level']."',  description = '".$_POST['description']."' WHERE ID = '".$_POST['update_level']."'");
    	redirect_header('index.php?op=level&currentID=2', 1, 'You have successfully updated the selected pledge level!');
	}






}




?>